package com.zygk.library.util;

/**
 * 查询本周、本月等的起始日期
 * Created by qhj on 2018/6/20 0020.
 */

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class SearchDate {

  private String startTime;
  private String endTime;

  public SearchDate(int index){
    int y, m, d;

    Calendar c = Calendar.getInstance();
    y = c.get(Calendar.YEAR);
    m = c.get(Calendar.MONTH) + 1;
    d = c.get(Calendar.DAY_OF_MONTH);

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

    startTime ="";
    endTime = "";


    switch(index){
      case 0://今天
        c = Calendar.getInstance();
        startTime = format.format(c.getTime());
        endTime = format.format(c.getTime());

        break;
      case 1://本周
        c = Calendar.getInstance();
        int day_of_week = c.get(Calendar. DAY_OF_WEEK) - 1;
        if (day_of_week == 0 ) {
          day_of_week = 7 ;
        }
        c.add(Calendar.DATE , -day_of_week + 1 );
        startTime = format.format(c.getTime());

        c = Calendar.getInstance();
        int day = c.get(Calendar. DAY_OF_WEEK) - 1;
        if (day == 0 ) {
          day = 7 ;
        }
        c.add(Calendar.DATE , -day + 7 );

        endTime = format.format(c.getTime());

        break;
      case 2:
        c = Calendar.getInstance();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.add(Calendar.DATE, -7);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        startTime = format.format(c.getTime());

        c = Calendar.getInstance();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.add(Calendar.DATE, -7);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        endTime = format.format(c.getTime());
        break;
      case 3://本月
        c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
        startTime = format.format(c.getTime());

        c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_MONTH,
            c.getActualMaximum(Calendar.DAY_OF_MONTH));
        endTime = format.format(c.getTime());
        break;
      case 4://本季度
        if(m<=3){
          startTime = y + "-1-1";
          endTime = y + "-3-31";
        }else if(m<=6){
          startTime = y + "-4-1";
          endTime = y + "-6-30";
        }else if(m<=9){
          startTime = y + "-7-1";
          endTime = y + "-9-30";
        }else {
          startTime = y + "-10-1";
          endTime = y + "-12-31";
        }
        break;
      case 5://本年
        c = Calendar.getInstance();
        startTime = y + "-1-1";
        endTime = y + "-12-31";
        break;
    }
  }

  public String getStartTime() {
    return startTime;
  }

  public String getEndTime() {
    return endTime;
  }
}
